// miniprogram/pages/details/details.js
import Notify from '@vant/weapp/notify/notify';
const db = wx.cloud.database()
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typedatas: [{
        name: '醴陵三三',
        icon: '../../icon/gongzi.png',
        active: false
      },
      {
        name: '无锡宝通',
        icon: '../../icon/jianzhi.png',
        active: false
      },
      {
        name: '河北一川',
        icon: '../../icon/lijin.png',
        active: false
      },
      {
        name: '福建明信',
        icon: '../../icon/jiangjin.png',
        active: false
      },

    ],
    show: false,
    shows: 0,
    money:1,
    minDate: new Date().getTime() - 864000000,
    defaultdate: new Date().getTime(),
    maxDate: new Date().getTime(),
 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.data)
    this.setData({
      ck_id: options.data
    })
    this.shu_data()
    var myDate = new Date();
    console.log(myDate.toLocaleString())
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  showPopup() {
    this.setData({
      show2: true
    });
  },
  onClose2() {
    this.setData({
      show2: false
    });
  },
  onClose3() {
    this.setData({
      show3: false
    });
  },
  onClose4() {
    this.setData({
      show4: false
    });
  },
  onConfirm(event) {
    console.log(this.formatDate(event.detail))
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
    });
  },
  calendar() {
    this.setData({
      show: true
    });
  },
  formatDate(date) {
    const dt = new Date(date);
    const y = dt.getFullYear();
    const m = (dt.getMonth() + 1 + "").padStart(2, "0");
    const d = (dt.getDate() + "").padStart(2, "0");
    return `${y}-${m}-${d}`;
  },
  onClick2(event) {
    const {
      name
    } = event.currentTarget.dataset;
    console.log(name)
    this.setData({
      recordtype: name,
      show2: false
    });
  },
  add_show() {
    this.setData({
      show3: true
    })
  },
  ck_reduce(event) {
    console.log(event.currentTarget.dataset.item)
    const data = event.currentTarget.dataset.item
    this.setData({
      show4: true,
      money: data.money,
      ck_data: data
    })
  },
  ck_reduce2() {
    var myDate = new Date();
    console.log(myDate.toLocaleString())
    var money = this.data.ck_data.money - this.data.money
    console.log(this.data.ck_list)
    var data2 = new Array()
    if (this.data.ck_data.lsdata != []) {
      data2 = this.data.ck_data.lsdata
    }
    let data = {
      money: this.data.money,
      yongtu: this.data.yongtu,
      date: myDate.toLocaleString()
    }
    data2.push(
      data
    )
    db.collection('record2').doc(
      this.data.ck_data._id,
    ).update({
      data: {
        money: money,
        yongtu: this.data.yongtu,
        lsdata: data2
      },
    }).then(res => {
      this.setData({
        show4: false
      })
      let da = this.data.ck_data
      let data = {
        ck_id: da.ck_id,
        ckc_id: da._id,
        date: da.date,
        ck_date: myDate.toLocaleString(),
        money: this.data.money,
        userid: wx.getStorageSync('id'),
        type: "ck",
        wuliao: da.wuliao,
        yongtu: da.yongtu,
        recordtype: da.recordtype
      }
      db.collection('record').add({
        data: data
      })
      this.shu_data()
    })

  },
  shu_data() {
    db.collection('record2').orderBy('date', 'desc').where({
      ck_id: this.data.ck_id
    }).get().then(res => {
      this.setData({
        ck_list: res.data
      })
      console.log(res)
    })

    db.collection('record').orderBy('date', 'desc').where({
      ck_id: this.data.ck_id
    }).get().then(res => {
      this.setData({
        ck_list_sum: res.data
      })
      console.log(res)
    })
    db.collection('ck').where({
      _id: this.data.ck_id
    }).get().then(res => {
      this.setData({
        wuliao: res.data[0].wuliao,
        ck_xx: res.data[0]
      })
    })
  },
  onChange(event) {
    this.setData({
      money: event.detail
    });
  },
  onSelect(event) {


    this.setData({
      shows: event.detail.name,

    });


  },
  preservation() {
    if (!this.data.recordtype) {
      Notify({
        type: 'warning',
        message: '请选择类型哦！'
      });
    } else if (!this.data.date) {
      Notify({
        type: 'warning',
        message: '请选择日期哦！'
      });
    } else if (!this.data.money) {
      Notify({
        type: 'warning',
        message: '请输入数量哦！'
      });
    } else {
      this.setData({
        loading: true
      });
      let postdata = {
        type: "jc",
        ck_id: this.data.ck_id,
        cc_name: this.data.cc_name,
        recordtype: this.data.recordtype,
        didi: this.data.didi,
        wuliao: this.data.wuliao,
        yongtu: this.data.yongtu,
        date: this.data.date,
        remarks: this.data.message,
        money: this.data.type == '0' ? this.data.money * -1 : Number(this.data.money),
        lsmoney: this.data.type == '0' ? this.data.money * -1 : Number(this.data.money),
        userid: wx.getStorageSync('id'),
      }
      db.collection('ck').where({
        _id: this.data.ck_id
      }).update({ data:{jc_shuangliang:this.data.ck_xx.money+this.data.money }
      })
      db.collection('record2').add({
        data: postdata
      }).then(res => {
        Notify({
          type: 'success',
          message: '增加成功'
        });
        this.setData({
          show3: false
        })
        this.shu_data()
      })
      db.collection('record').add({
        data: postdata
      })

    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})